<?php
namespace app\manage\controller;
use think\Db;

class ChartData extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
        }else{
            $date = date('Y-m-d', time()-86400).' - '.date('Y-m-d', time()-86400);
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            return view('', ['date'=>$date, 'today'=>$today]);
        }
    }

    public function gameRank(){
        if( request()->isAjax() ){
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $type = input('type');

            if( strtotime(date('Y-m-d', $date['end_time'])) == strtotime(date('Y-m-d', time())) ){
                $selectGame = model('Data')->selectGameNow($plat, $date['start_time'], $date['end_time'], '', '', $type);
            }else{
                $selectGame = model('Data')->selectGameOld($plat, $date['start_time'], $date['end_time'], '', '', $type);
            }

            $title = [];
            $all = [];
            $i = 1;
            foreach ($selectGame['data'] as $k=>$v){
                if($type=='game_add'){
                    if(isset($v['game_add']) && (int)$v['game_add']!=0){
                        $all[0]['name'] = '游戏新增（人）';
                        $all[0]['data'][] = (int)$v['game_add'];
                        $title[] = $v['game_id'] . '：' . $v['game_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_add']/$selectGame['sum']['game_add']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='game_role'){
                    if(isset($v['game_role']) && (int)$v['game_role']!=0){
                        $all[0]['name'] = '游戏创角（人）';
                        $all[0]['data'][] = (int)$v['game_role'];
                        $title[] = $v['game_id'] . '：' . $v['game_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_role']/$selectGame['sum']['game_role']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='game_active'){
                    if(isset($v['game_active']) && (int)$v['game_active']!=0){
                        $all[0]['name'] = '游戏活跃（人）';
                        $all[0]['data'][] = (int)$v['game_active'];
                        $title[] = $v['game_id'] . '：' . $v['game_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_active']/$selectGame['sum']['game_active']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='pay_number'){
                    if(isset($v['pay_number']) && (int)$v['pay_number']!=0){
                        $all[0]['name'] = '付费人数（人）';
                        $all[0]['data'][] = (int)$v['pay_number'];
                        $title[] = $v['game_id'] . '：' . $v['game_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['pay_number']/$selectGame['sum']['pay_number']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='pay_money'){
                    if(isset($v['pay_money']) && (int)$v['pay_money']!=0){
                        $all[0]['name'] = '付费金额（元）';
                        $all[0]['data'][] = (int)$v['pay_money'];
                        $title[] = $v['game_id'] . '：' . $v['game_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['pay_money']/$selectGame['sum']['pay_money']*100, 2)) . '%</strong>';
                    }
                }
            }

            echo json_encode(['title'=>$title, 'all'=>$all]);
        }else{
            $date = date('Y-m-d', time()-86400).' - '.date('Y-m-d', time()-86400);
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            return view('', ['date'=>$date, 'today'=>$today]);
        }
    }

    public function channelRank(){
        if( request()->isAjax() ){
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $type = input('type');

            if( strtotime(date('Y-m-d', $date['end_time'])) == strtotime(date('Y-m-d', time())) ){
                $selectChannel = model('Data')->selectChannelNow($plat, $date['start_time'], $date['end_time'], '', '', $type);
            }else{
                $selectChannel = model('Data')->selectChannelOld($plat, $date['start_time'], $date['end_time'], '', '', $type);
            }

            $title = [];
            $all = [];
            $i = 1;
            foreach ($selectChannel['data'] as $k=>$v){
                if($type=='game_add'){
                    if(isset($v['game_add']) && $v['game_add']!=0){
                        $all[0]['name'] = '游戏新增（人）';
                        $all[0]['data'][] = (int)$v['game_add'];
                        $title[] = $v['channel_id'] . '：' . $v['channel_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_add']/$selectChannel['sum']['game_add']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='game_role'){
                    if(isset($v['game_role']) && $v['game_role']!=0){
                        $all[0]['name'] = '游戏创角（人）';
                        $all[0]['data'][] = (int)$v['game_role'];
                        $title[] = $v['channel_id'] . '：' . $v['channel_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_role']/$selectChannel['sum']['game_role']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='game_active'){
                    if(isset($v['game_active']) && $v['game_active']!=0){
                        $all[0]['name'] = '游戏活跃（人）';
                        $all[0]['data'][] = (int)$v['game_active'];
                        $title[] = $v['channel_id'] . '：' . $v['channel_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['game_active']/$selectChannel['sum']['game_active']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='pay_number'){
                    if(isset($v['pay_number']) && $v['pay_number']!=0){
                        $all[0]['name'] = '付费人数（人）';
                        $all[0]['data'][] = (int)$v['pay_number'];
                        $title[] = $v['channel_id'] . '：' . $v['channel_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['pay_number']/$selectChannel['sum']['pay_number']*100, 2)) . '%</strong>';
                    }
                }
                if($type=='pay_money'){
                    if(isset($v['pay_money']) && $v['pay_money']!=0){
                        $all[0]['name'] = '付费金额（元）';
                        $all[0]['data'][] = (int)$v['pay_money'];
                        $title[] = $v['channel_id'] . '：' . $v['channel_name'] . ' — 排名 <strong>' . $i++ . "</strong> — 占比 <strong>" . sprintf('%.2f', round($v['pay_money']/$selectChannel['sum']['pay_money']*100, 2)) . '%</strong>';
                    }
                }
            }

            echo json_encode(['title'=>$title, 'all'=>$all]);
        }else{
            $date = date('Y-m-d', time()-86400).' - '.date('Y-m-d', time()-86400);
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            return view('', ['date'=>$date, 'today'=>$today]);
        }
    }
}